home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_04 / janzen / janzen.fzy < prev   
Text File  |  1994-03-08  |  21KB  |  817 lines

  1. // window.cxx
  2. // This is window.cxx, a C++ implementation of Bart Kosko's fuzzy associative memory
  3. // and also fuzzy values, fuzzy sets and fuzzy membership functions for fuzzy logic.
  4. // This software is provided without warranty and with no liability by
  5. // Thomas E. Janzen
  6. // 208A Olde Derby Rd
  7. // Norwood, MA  02062
  8. // tej@world.std.com
  9. //
  10. // This is an improved version of code provided in C Users Journal November 1993.
  11. //   12 December 93
  12. //  To create this file, the examples in the article were merged in order by example 
  13. //    number; then changes were made.
  14. // #include directives repeated in each example were removed from examples 2,3,4,5.
  15. // One error found in the article code was line 35 in example 4.  The membership
  16. //   member function was declared with an implied int argument. 
  17. //   This should be a fuzzy.
  18. // Other changes made here:
  19. //  exit(0) was replaced with return 0L because of a compiler requirement for a return
  20. //    from main()
  21. //  "partly closed" was completely removed from the memberships for window position. 
  22. //    It was probably conceptually misled.
  23. //  Since the closed position of the window is at zero inches, and near the end
  24. //    the fuzzy membership is membership is multiplied by zero, the "closed" 
  25. //    membership object doesn't do much, but in the divisor in the third-to-last
  26. //    statement it has some effect.
  27. //  Some inline modifiers were added to a few functions.
  28. //  Comments were added to explain the numeric values used.
  29. //  Command line arguments are used.  Type window [temperature].  If temperature
  30. //      is omitted, a prompt for it will appear.
  31. //  The numeric values for window position were changed in the membership functions.
  32. //      The window opening now seems more or less reasonable.  The idea was to open
  33. //      the window only in mild weather, and to close it in cold and hot weather.
  34. //
  35. // Fuzzy references:
  36. //
  37. // Klir, George J.; Folger, Tina A.  1988.  Fuzzy Sets, Uncertainty, 
  38. //  and Information.  Prentice Hall. Englewood Cliffs.
  39. // Kosko, Bart.  1992.  Neural Networks and Fuzzy Systems, A 
  40. //  Dynamical Systems Approach to Machine Intelligence.   Prentice 
  41. //  Hall.  Englewood Cliffs.
  42. // Results:
  43. // For temperature: 0 open the window 0 inches
  44. // For temperature: 10 open the window 2.25728 inches
  45. // For temperature: 20 open the window 7.5 inches
  46. // For temperature: 30 open the window 7.5 inches
  47. // For temperature: 40 open the window 7.5 inches
  48. // For temperature: 50 open the window 9.82819 inches
  49. // For temperature: 60 open the window 10.9489 inches
  50. // For temperature: 70 open the window 10.9489 inches
  51. // For temperature: 80 open the window 7.5 inches
  52. // For temperature: 90 open the window 0 inches
  53.  
  54. #include <iostream.h>
  55. #include <math.h>
  56. #include <stdlib.h>
  57.  
  58. class fuzzy
  59. {
  60.     private:
  61.         double  truth;
  62.     public:
  63.         inline fuzzy(const double);
  64.         void get(double *);
  65.         inline double get(void) const;
  66.         inline fuzzy operator!(void) const;
  67.         inline fuzzy very(void) const;
  68.         inline fuzzy somewhat(void) const;
  69.         inline fuzzy& operator|=(fuzzy&);
  70.         inline fuzzy& operator&=(fuzzy&);
  71.         fuzzy& operator=(const fuzzy&);
  72.         fuzzy& operator=(const double);
  73.         friend fuzzy operator|(const fuzzy&, 
  74.                                const fuzzy&);
  75.         friend fuzzy operator&(const fuzzy&,
  76.                                const fuzzy&);
  77.         friend fuzzy fuzzy_implies(const fuzzy&,
  78.                                    const fuzzy&);
  79.         friend fuzzy fuzzy_iff(const fuzzy&,
  80.                                const fuzzy&);
  81.         friend inline int operator<(const fuzzy&,
  82.                                     const fuzzy&);
  83.         friend inline int operator<=(const fuzzy&,
  84.                                      const fuzzy&);
  85.         friend inline int operator==(const fuzzy&,
  86.                                      const fuzzy&);
  87.         friend inline int operator>=(const fuzzy&,
  88.                                      const fuzzy&);
  89.         friend inline int operator>(const fuzzy&,
  90.                                     const fuzzy&);
  91.         friend inline int operator!=(const fuzzy&,
  92.                                      const fuzzy&);
  93.         friend ostream& operator<<(ostream& s, 
  94.                                    const fuzzy& z);
  95. };
  96.  
  97. inline fuzzy::fuzzy(const double temp_truth = 0.0)
  98. {
  99.     truth = ((temp_truth >= 0.0) 
  100.           && (temp_truth <= 1.0)) ?
  101.               temp_truth : 0.0;
  102.     return;
  103. }
  104.  
  105. inline void fuzzy::get(double *get_truth)
  106. {
  107.     *get_truth = truth;
  108.     return;
  109. }
  110.  
  111. double fuzzy::get(void) const
  112. {
  113.     return truth;
  114. }
  115.  
  116. inline fuzzy fuzzy::operator!(void) const
  117. {
  118.     return (1.0 - truth);
  119. }
  120.  
  121. inline fuzzy fuzzy::very(void) const
  122. {
  123.     return (truth * truth);
  124. }
  125.  
  126. inline fuzzy fuzzy::somewhat(void) const
  127. {
  128.     return sqrt(truth);
  129. }
  130.  
  131. inline fuzzy& fuzzy::operator|=(fuzzy& value)
  132. {
  133.     return (*this = *this | value);
  134. }
  135.  
  136. inline fuzzy& fuzzy::operator&=(fuzzy& value)
  137. {
  138.     return (*this = *this & value);
  139. }
  140.  
  141. fuzzy& fuzzy::operator=(const fuzzy& fuzzy_in)
  142. {
  143.     truth = fuzzy_in.truth;
  144.     return *this;
  145. }
  146.  
  147. fuzzy& fuzzy::operator=(const double dbl_in)
  148. {
  149.     truth = ((dbl_in >= 0.0) && (dbl_in <= 1.0)) ?
  150.               dbl_in : 0.0;
  151.     return *this;
  152. }
  153.  
  154. fuzzy operator|(const fuzzy& value_a, const fuzzy& value_b)
  155. {
  156.     return (value_a.truth > value_b.truth) ?
  157.             value_a : value_b;
  158. }
  159.  
  160. fuzzy operator&(const fuzzy &value_a, 
  161.                 const fuzzy &value_b)
  162. {
  163.     return (value_a < value_b) ? value_a : value_b;
  164. }
  165.  
  166. fuzzy fuzzy_implies(const fuzzy& value_a, 
  167.                     const fuzzy& value_b)
  168. {
  169.     auto double temp_value;
  170.     
  171.     temp_value = 1.0 - value_a.truth + value_b.truth;
  172.     return ((temp_value < 1.0) ? temp_value : 1.0);
  173. }
  174.  
  175. fuzzy fuzzy_iff(const fuzzy& value_a, 
  176.                 const fuzzy& value_b)
  177. {
  178.     return 1.0 - fabs(value_a.truth - value_b.truth);
  179. }
  180.  
  181. inline int operator<(const fuzzy& value_a, 
  182.                      const fuzzy& value_b)
  183. {
  184.     return (value_a.truth < value_b.truth);
  185. }
  186.  
  187. inline int operator<=(const fuzzy& value_a, 
  188.                       const fuzzy& value_b)
  189. {
  190.     return (value_a.truth <= value_b.truth);
  191. }
  192.  
  193. inline int operator==(const fuzzy& value_a,
  194.                       const fuzzy& value_b)
  195. {
  196.     return (value_a.truth == value_b.truth);
  197. }
  198.  
  199. inline int operator>=(const fuzzy& value_a,
  200.                       const fuzzy& value_b)
  201. {
  202.     return (value_a.truth >= value_b.truth);
  203. }
  204.  
  205. inline int operator>(const fuzzy& value_a,
  206.                      const fuzzy& value_b)
  207. {
  208.     return (value_a.truth > value_b.truth);
  209. }
  210.  
  211. inline int operator!=(const fuzzy& value_a,
  212.                       const fuzzy& value_b)
  213. {
  214.     return (value_a.truth != value_b.truth);
  215. }
  216.  
  217. ostream& operator<<(ostream& s, const fuzzy& z)
  218. {
  219.     return s << z.truth;
  220. }
  221.  
  222. istream& operator>>(istream& s, fuzzy& fur)
  223. {
  224.     auto double lint = 0.0;
  225.     
  226.     s >> lint;
  227.     fur = lint;
  228.  
  229.     return s;
  230. }
  231.  
  232. class fzy_set
  233. {
  234.     private:
  235.         int dimension;
  236.         fuzzy *fzy_data;
  237.     public:
  238.         fzy_set(const int);
  239.         fzy_set(const fzy_set&);
  240.         ~fzy_set(void);
  241.         fzy_set& operator=(const fzy_set&);
  242.         fuzzy& operator[](const int len);
  243.         fuzzy& operator[](const int len) const;
  244.         friend int operator<(const fzy_set&, 
  245.                              const fzy_set&);
  246.         friend int operator<=(const fzy_set&, 
  247.                               const fzy_set&);
  248.         friend int operator==(const fzy_set&,
  249.                               const fzy_set&);
  250.         friend int operator>=(const fzy_set&, 
  251.                               const fzy_set&);
  252.         friend int operator>(const fzy_set&, 
  253.                              const fzy_set&);
  254.         friend int operator!=(const fzy_set&, 
  255.                               const fzy_set&);
  256.         friend ostream& operator<<(ostream& s, 
  257.                                    fzy_set& z);
  258.         friend fuzzy fzy_set_max(const fzy_set&);
  259.         friend fzy_set operator*(const fzy_set&, 
  260.                                  const class fam_2&);
  261. };
  262.  
  263. fzy_set::fzy_set(const int size = 1) // constructor
  264. {
  265.     dimension = ((size >= 0) ? size : 1);
  266.     fzy_data = new fuzzy[dimension];
  267.     return;
  268. }
  269.  
  270. fzy_set::f